﻿using System.Drawing;
using System.Windows.Forms;

namespace JJware.Schedule
{
    public enum DividerOrientation
    {
        Vertical, Horizontal
    }

    public partial class Divider : UserControl
    {
        DividerOrientation _Orientation;
        Color _Color1;
        Color _Color2;

        public Divider()
        {
            InitializeComponent();
            _Orientation = DividerOrientation.Horizontal;
        }

        private void Divider_Paint(object sender, PaintEventArgs e)
        {
            DrawDivider(e.Graphics);
        }

        private void DrawDivider(Graphics graphics)
        {
            Point point1 = new Point();
            Point point2 = new Point();
            int valueToAdd = 0;
            if (_Orientation == DividerOrientation.Horizontal)
            {
                if (Width < Height) {
                    int value1 = Width;
                    int value2 = Height;
                    Swap(ref value1, ref value2);
                    Width = value1;
                    Height = value2;
                }
                point1 = new Point(0, Height / 2);
                point2 = new Point(Width, Height / 2);
                valueToAdd = 1;
            }
            else if (_Orientation == DividerOrientation.Vertical)
            {
                if (Height < Width)
                {
                    int value1 = Width;
                    int value2 = Height;
                    Swap(ref value1, ref value2);
                    Width = value1;
                    Height = value2;
                }
                point1 = new Point(Width / 2, 0);
                point2 = new Point(Width / 2, Height);
                valueToAdd = -1;
            }
            graphics.DrawLine(new Pen(new SolidBrush(_Color1)),
                   point1, point2);
            if (valueToAdd == 1)
            {
                point1.Y += valueToAdd;
                point2.Y += valueToAdd;
            }
            else if (valueToAdd == -1)
            {
                point1.X += valueToAdd;
                point2.X += valueToAdd;
            }
            graphics.DrawLine(new Pen(new SolidBrush(_Color2)),
            point1, point2);
            
        }

        private void Swap(ref int value1, ref int value2) {
            int appo = value1;
            value1 = value2;
            value2 = appo;
        }

        public DividerOrientation Orientation
        {
            get { return _Orientation; }
            set { _Orientation = value; }
        }

        public Color Color1
        {
            get { return _Color1; }
            set { _Color1 = value; }
        }

        public Color Color2
        {
            get { return _Color2; }
            set { _Color2 = value; }
        }
    }
}
